package vn.fpt.fsoft.coreframework;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

/**
 * @author DoN
 * 
 */
public class Player {

	public static final int DIRECTION_UP = 0;
	public static final int DIRECTION_RIGHT = 1;
	public static final int DIRECTION_DOWN = 2;
	public static final int DIRECTION_LEFT = 3;

	 int direction = DIRECTION_UP;
	// Properties
	private String name; // Tên của player
	private Point position; // Vị trí hiện tại của player
	private Color color; // Màu sắc của player
	private int hitPoint; // Số lương máu của player
	protected BufferedImage bufferedImage;
	protected Sprite sprite;

	// Contructor
	public Player(String name) {
		this.name = name;
		sprite = new Sprite();
//		this.bufferedImage = sprite.getSprite("images/tank1.png");
		
//		projectileManager = new ProjectileManager();
	}

	// Getter & Setter
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Point getPosition() {
		return position;
	}

	public void setPosition(Point position) {
		this.position = position;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public int getHitPoint() {
		return hitPoint;
	}

	public void setHitPoint(int hitPoint) {
		this.hitPoint = hitPoint;
	}
	
	/**
	 * @return the direction
	 */
	public int getDirection() {
		return direction;
	}

	/**
	 * @param direction
	 *            the direction to set
	 */
	public void setDirection(int direction) {
		this.direction = direction;
	}
	
	public Point getCenterPlayer(){
		 return new Point(position.x + bufferedImage.getWidth()/2, position.y + bufferedImage.getHeight()/2);
	}


	public void update(){
		
	}
	
	public void draw(Graphics2D g) {
		g.drawImage(bufferedImage, position.x, position.y, null);
	}

	public void initPlayer() {
		this.color = new Color(0, 0, 255);
		this.position = new Point(360, 280);
		this.hitPoint = 100;
		// this.missile = new Missile('B', this.position);
	}

	public boolean checkCollision() {
		return true;
	}

	public boolean checkProjecttile() {
		return true;
	}

	public boolean isDead() {
		return true;
	}
	
	public void move(int distance) {
		int x = position.x;
		int y = position.y;

		switch (direction) {
		case DIRECTION_UP:
			y -= distance;
			break;
		case DIRECTION_DOWN:
			y += distance;
			break;
		case DIRECTION_LEFT:
			x -= distance;
			break;
		case DIRECTION_RIGHT:
			x += distance;
			break;
		}
		setPosition(new Point(x, y));
	}

	public boolean changeDirection(int newDirection) {
		double angel = 0;
		boolean changed = direction != newDirection;
		int part = Math.abs(direction - newDirection);
		if (part == 1) {
			angel = Math.PI / 2;
		} else if (part == 2) {
			angel = Math.PI;
		} else if (part == 3) {
			angel = Math.PI * 1.5;
		} else {
			angel = 0;
		}
		
		if (direction - newDirection < 0) {
			bufferedImage = sprite.rotateImage(bufferedImage, angel);
		} else {
			bufferedImage = sprite.rotateImage(bufferedImage, -angel);
		}
		direction = newDirection;
		return changed;
	}
}
